package com.imist.mybatisdemo2.servlet;

import com.imist.mybatisdemo2.dao.UserDao;
import com.imist.mybatisdemo2.entity.User;
import org.apache.log4j.Logger;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet("/detail")
public class UserFindByIdServlet extends HttpServlet {
    /**
     * 创建对应的日志记录对象；通过不同的级别进行日志的记录；[Debug/Warn/info/Log]（级别由低到高）
     */
    private Logger logger = Logger.getLogger(UserFindByIdServlet.class);

    private UserDao userDao = new UserDao();

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req,resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String id = req.getParameter("id");
        logger.info("页面传过来的参数-->"+id);
        User user = userDao.findUserById(Integer.parseInt(id));
        logger.info("查询完成，得到用户：-->"+user.toString());
        req.setAttribute("user",user);
        req.getRequestDispatcher("detail.jsp").forward(req,resp);
    }
}
